﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Songhay.OpenXml.Collections
{
    /// <summary>
    /// Groups elements in a collection with adjacent elements based on a common key.
    /// </summary>
    /// <typeparam name="TSource">The type of the source.</typeparam>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <remarks>
    /// This class is from PowerTools for Open XML (http://powertools.codeplex.com/).
    /// 
    /// For more detail, see:
    /// 
    /// “The GroupAdjacent Extension Method” by Eric White
    /// http://blogs.msdn.com/b/ericwhite/archive/2008/04/21/the-groupadjacent-extension-method.aspx
    /// 
    /// </remarks>
    public class GroupOfAdjacent<TSource, TKey> : IEnumerable<TSource>, IGrouping<TKey, TSource>
    {
        /// <summary>
        /// Gets or sets the key.
        /// </summary>
        /// <value>The key.</value>
        public TKey Key { get; set; }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((System.Collections.Generic.IEnumerable<TSource>)this).GetEnumerator();
        }

        IEnumerator<TSource>
            IEnumerable<TSource>.GetEnumerator()
        {
            foreach(var s in _groupList)
                yield return s;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupOfAdjacent&lt;TSource, TKey&gt;"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="key">The key.</param>
        public GroupOfAdjacent(List<TSource> source, TKey key)
        {
            _groupList = source;
            Key = key;
        }

        List<TSource> _groupList { get; set; }
    }
}
